// ignore_for_file: must_be_immutable

import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/radius_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:aitao_flutter/utils/alert_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../common_ui/empty_view.dart';
import '../model/hello_voice_model.dart';
import '../request/api.dart';
import '../request/requestManager.dart';
import '../style/edgeInsets_style.dart';
import '../utils/log_utils.dart';

class GreetVoicePage extends StatefulWidget {
  GreetVoicePage(
      {super.key,
      required this.greetVoiceCallback,
      required this.deleteVoiceCallback});
  Function(int) greetVoiceCallback;
  Function(int) deleteVoiceCallback;
  @override
  State<GreetVoicePage> createState() => _GreetVoicePageState();
}

class _GreetVoicePageState extends State<GreetVoicePage> {
  List<HelloVoiceModel> voiceList = [];

  @override
  Widget build(BuildContext context) {
    return voiceList.isEmpty
        ? EmptyView()
        : ListView.builder(
            // itemExtent: 70,
            padding: EdgeInsets.zero,
            itemBuilder: (context, index) {
              return Padding(
                  padding: EdgeInsetsStyle.only(top: 10, bottom: 10),
                  child: setHelloVoictStyle(index));
            },
            itemCount: voiceList.length,
          );
  }

  Widget setHelloVoictStyle(int index) {
    HelloVoiceModel model = voiceList[index];
    return GestureDetector(
      child: Container(
        padding: const EdgeInsets.fromLTRB(50, 20, 50, 20),
        color: ColorStyle.whiteColor,
        child: Row(children: [
          Expanded(
              child: Container(
            height: 44,
            padding: const EdgeInsets.fromLTRB(13, 7, 16, 7),
            decoration: BoxDecoration(
                borderRadius: RadiusStyle.setPortionRadius(
                    PortionRaduisType.raduisTopRightAndBottom, 6),
                color: model.isAudit == 1
                    ? ColorStyle.mainColor
                    : const Color(0xFFDDDDDD)),
            child: Row(
              children: [
                Image.asset(ImagePath.assetsImVoice),
                Box.hBox10,
                Image.asset(ImagePath.assetsImVoiceWhite3),
                Box.hBox6,
                Text(
                  model.isAudit == 1 ? '' : '审核中',
                  style: Styles.greyText_14,
                ),
                const Spacer(),
                Text(
                  '${model.duration}s',
                  style: Styles.whiteText_14,
                ),
              ],
            ),
          )),
          Box.hBox30,
          GestureDetector(
            child: Text(
              '删除',
              style: Styles.mainText_14,
            ),
            onTap: () {
              AlertUtils().showCenterTipsDialog(context, (e) {
                debugPrint(e);
                widget.deleteVoiceCallback(model.hiId ?? 0);
                // deleteGreetVoice(model.hiId ?? 0);
              }, '提示', '是否删除该条语音招呼', option1: '删除');
            },
          )
        ]),
      ),
      onTap: () {
        if (voiceList[index].isAudit == 0) {
          EasyLoading.showToast('招呼审核中...');
          debugPrint('招呼审核中...');
          return;
        }
        widget.greetVoiceCallback(voiceList[index].hiId ?? 0);
      },
    );
  }

  @override
  void initState() {
    super.initState();
    getVocieListData();
  }

  void getVocieListData() async {
    var result = await RequestManager().post(
      API.getSayHelloVoice,
    );
    if (result != null && result['code'] == 0) {
      List tempList = result['data']['list'];
      debugPrint('获取打招呼语音列表');
      WJLogUtils.wjLog(result);
      if (tempList.isNotEmpty) {
        setState(() {
          voiceList = tempList.map((e) {
            return HelloVoiceModel.fromJson(e);
          }).toList();
        });
      }
    }
  }
}
